<template>
  <div class="map-container">
    <!-- 百度三维地图容器 -->
    <div id="baidu-map" class="baidu-map"></div>
    
    <!-- 左侧悬浮框 -->
    <div class="left-panels">
      <!-- 左上角框 -->
      <div class="panel panel-top-left">
        <div class="panel-header">
          <h3>传感器监测</h3>
        </div>
        <div class="panel-content">
          <div class="sensor-item">
            <span class="sensor-label">温度传感器</span>
            <span class="sensor-value">25.6°C</span>
          </div>
          <div class="sensor-item">
            <span class="sensor-label">压力传感器</span>
            <span class="sensor-value">101.3kPa</span>
          </div>
          <div class="sensor-item">
            <span class="sensor-label">液位传感器</span>
            <span class="sensor-value">78%</span>
          </div>
        </div>
      </div>
      
      <!-- 左下角框 -->
      <div class="panel panel-bottom-left">
        <div class="panel-header">
          <h3>报警信息</h3>
        </div>
        <div class="panel-content">
          <div class="alarm-item">
            <span class="alarm-type warning">警告</span>
            <span class="alarm-desc">温度过高</span>
          </div>
          <div class="alarm-item">
            <span class="alarm-type danger">紧急</span>
            <span class="alarm-desc">压力异常</span>
          </div>
          <div class="alarm-item">
            <span class="alarm-type info">信息</span>
            <span class="alarm-desc">设备维护提醒</span>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 右侧悬浮框 -->
    <div class="right-panels">
      <!-- 右上角框 -->
      <div class="panel panel-top-right">
        <div class="panel-header">
          <h3>设备状态</h3>
        </div>
        <div class="panel-content">
          <div class="device-item">
            <div class="device-status online"></div>
            <span class="device-name">泵站A</span>
            <span class="device-status-text">运行中</span>
          </div>
          <div class="device-item">
            <div class="device-status offline"></div>
            <span class="device-name">泵站B</span>
            <span class="device-status-text">离线</span>
          </div>
          <div class="device-item">
            <div class="device-status online"></div>
            <span class="device-name">阀门C</span>
            <span class="device-status-text">运行中</span>
          </div>
        </div>
      </div>
      
      <!-- 右下角框 -->
      <div class="panel panel-bottom-right">
        <div class="panel-header">
          <h3>实时数据</h3>
        </div>
        <div class="panel-content">
          <div class="data-item">
            <span class="data-label">流量</span>
            <span class="data-value">125.6 m³/h</span>
          </div>
          <div class="data-item">
            <span class="data-label">能耗</span>
            <span class="data-value">45.2 kW</span>
          </div>
          <div class="data-item">
            <span class="data-label">运行时间</span>
            <span class="data-value">156h</span>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 可以在地图上方添加其他内容 -->
    <div class="overlay-content">
      <h1>化工安全监控平台</h1>
      <p>基于百度三维地图的实时监控系统</p>
      
      <div class="map-controls">
        <button @click="zoomIn" class="control-btn">
          <i class="fa fa-search-plus"></i> 放大
        </button>
        <button @click="zoomOut" class="control-btn">
          <i class="fa fa-search-minus"></i> 缩小
        </button>
        <button @click="resetMap" class="control-btn">
          <i class="fa fa-refresh"></i> 重置
        </button>
        <button @click="toggle3D" class="control-btn">
          <i class="fa fa-cube"></i> {{ is3DEnabled ? '2D模式' : '3D模式' }}
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'BaiduMap3DBackground',
  data() {
    return {
      map: null,
      is3DEnabled: true,
      // 地图初始配置
      mapConfig: {
        center: { lng: 116.404, lat: 39.915 }, // 北京坐标
        zoom: 15,
        minZoom: 3,
        maxZoom: 20,
        pitch: 60, // 3D视角俯仰角
        heading: 0 // 3D视角方向角
      }
    }
  },
  mounted() {
    // 初始化三维地图
    this.init3DMap()
    
    // 监听窗口大小变化，调整地图尺寸
    window.addEventListener('resize', this.handleResize)
    
    // 初始调整一次地图尺寸
    this.handleResize()
  },
  beforeDestroy() {
    // 移除事件监听
    window.removeEventListener('resize', this.handleResize)
  },
  methods: {
    // 初始化百度三维地图
    init3DMap() {
      // 检查百度地图API是否加载完成
      if (typeof BMapGL === 'undefined') {
        console.error('百度地图GL API加载失败，请检查密钥和版本是否正确')
        // 尝试使用普通API作为备选
        this.init2DMap()
        return
      }
      
      // 创建三维地图实例
      this.map = new BMapGL.Map('baidu-map', {
        enableAutoResize: true
      })
      
      // 创建点坐标
      const point = new BMapGL.Point(
        this.mapConfig.center.lng,
        this.mapConfig.center.lat
      )
      
      // 初始化地图，设置中心点坐标和地图级别
      this.map.centerAndZoom(point, this.mapConfig.zoom)
      
      // 设置3D视角
      this.map.setHeading(this.mapConfig.heading)
      this.map.setPitch(this.mapConfig.pitch)
      this.map.setTilt(75) // 设置倾斜角度
      
      // 启用滚轮缩放
      this.map.enableScrollWheelZoom(true)
      
      // 启用键盘操作
      this.map.enableKeyboard(true)
      
      // 启用双击缩放
      this.map.enableDoubleClickZoom(true)
      
      // 添加地图类型控件
      this.map.addControl(new BMapGL.MapTypeControl({
        mapTypes: [BMAP_NORMAL_MAP, BMAP_SATELLITE_MAP, BMAP_HYBRID_MAP]
      }))
      
      // 添加缩放控件
      this.map.addControl(new BMapGL.NavigationControl3D())
      
      // 添加比例尺控件
      this.map.addControl(new BMapGL.ScaleControl())
      
      // 添加定位控件
      this.map.addControl(new BMapGL.LocationControl())
      
      // 添加3D建筑物
      this.enable3DBuildings()
      
      // 可以在这里添加标记点
      this.add3DMarkers()
      
      // 添加地图事件监听
      this.addMapEvents()
    },
    
    // 备选的2D地图初始化
    init2DMap() {
      if (typeof BMap === 'undefined') {
        console.error('百度地图API加载失败，请检查密钥是否正确')
        return
      }
      
      this.map = new BMap.Map('baidu-map')
      const point = new BMap.Point(
        this.mapConfig.center.lng,
        this.mapConfig.center.lat
      )
      this.map.centerAndZoom(point, this.mapConfig.zoom)
      this.map.enableScrollWheelZoom(true)
      this.is3DEnabled = false
    },
    
    // 启用3D建筑物
    enable3DBuildings() {
      if (this.map && this.map.setDisplayOptions) {
        this.map.setDisplayOptions({
          building: true, // 显示3D建筑物
          indoor: false,  // 不显示室内地图
          skyColors: [    // 天空颜色渐变
            'rgba(226, 235, 255, 0)',
            'rgba(187, 214, 255, 0.7)'
          ]
        })
      }
    },
    
    // 添加3D标记点示例
    add3DMarkers() {
      if (!this.map) return
      
      // 创建3D标记点
      const marker = new BMapGL.Marker(
        new BMapGL.Point(this.mapConfig.center.lng, this.mapConfig.center.lat),
        {
          enableMassClear: true
        }
      )
      
      // 添加标记点到地图
      this.map.addOverlay(marker)
      
      // 创建信息窗口
      const infoWindow = new BMapGL.InfoWindow('这里是监控中心', {
        width: 200,
        height: 100,
        title: '中心位置'
      })
      
      // 为标记点添加点击事件
      marker.addEventListener('click', () => {
        this.map.openInfoWindow(infoWindow, marker.getPosition())
      })
      
      // 添加一些示例标记点
      this.addExampleMarkers()
    },
    
    // 添加示例标记点
    addExampleMarkers() {
      const points = [
        { lng: 116.414, lat: 39.925, title: '监控点A' },
        { lng: 116.394, lat: 39.905, title: '监控点B' },
        { lng: 116.424, lat: 39.935, title: '监控点C' }
      ]
      
      points.forEach(point => {
        const marker = new BMapGL.Marker(new BMapGL.Point(point.lng, point.lat))
        this.map.addOverlay(marker)
        
        // 添加简单点击事件
        marker.addEventListener('click', () => {
          const infoWindow = new BMapGL.InfoWindow(point.title)
          this.map.openInfoWindow(infoWindow, marker.getPosition())
        })
      })
    },
    
    // 添加地图事件监听
    addMapEvents() {
      if (!this.map) return
      
      // 地图加载完成事件
      this.map.addEventListener('tilesloaded', () => {
        console.log('三维地图加载完成')
      })
      
      // 地图点击事件
      this.map.addEventListener('click', (e) => {
        console.log('点击位置:', e.latlng)
      })
      
      // 地图视角变化事件
      this.map.addEventListener('pitch', (e) => {
        console.log('俯仰角变化:', e.pitch)
      })
      
      this.map.addEventListener('heading', (e) => {
        console.log('方向角变化:', e.heading)
      })
    },
    
    // 地图放大
    zoomIn() {
      if (this.map && this.map.getZoom() < this.mapConfig.maxZoom) {
        this.map.zoomIn()
      }
    },
    
    // 地图缩小
    zoomOut() {
      if (this.map && this.map.getZoom() > this.mapConfig.minZoom) {
        this.map.zoomOut()
      }
    },
    
    // 重置地图
    resetMap() {
      if (this.map) {
        const point = new (this.is3DEnabled ? BMapGL : BMap).Point(
          this.mapConfig.center.lng,
          this.mapConfig.center.lat
        )
        this.map.centerAndZoom(point, this.mapConfig.zoom)
        
        if (this.is3DEnabled) {
          this.map.setHeading(this.mapConfig.heading)
          this.map.setPitch(this.mapConfig.pitch)
        }
      }
    },
    
    // 切换2D/3D模式
    toggle3D() {
      if (this.is3DEnabled) {
        // 切换到2D
        this.is3DEnabled = false
        if (this.map && this.map.setPitch) {
          this.map.setPitch(0)
          this.map.setTilt(0)
        }
      } else {
        // 切换到3D
        this.is3DEnabled = true
        if (this.map && this.map.setPitch) {
          this.map.setPitch(this.mapConfig.pitch)
          this.map.setTilt(75)
        }
      }
    },
    
    // 处理窗口大小变化
    handleResize() {
      if (this.map) {
        setTimeout(() => {
          this.map.resize()
        }, 100)
      }
    }
  }
}
</script>

<style scoped>
.map-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.baidu-map {
  width: 100%;
  height: 100%;
  z-index: 1;
}

/* 悬浮面板容器 */
.left-panels,
.right-panels {
  position: absolute;
  top: 0;
  height: 100%;
  z-index: 10;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 20px;
  pointer-events: none;
  box-sizing: border-box;
}

.left-panels {
  left: 0;
  align-items: flex-start;
}

.right-panels {
  right: 200px;
  align-items: flex-end;
}

/* 面板通用样式 */
.panel {
  width: 480px;
  background: linear-gradient(135deg, 
    rgba(11, 77, 124, 0.65) 0%,
    rgba(11, 77, 124, 0.5) 50%,
    rgba(11, 77, 124, 0.3) 100%);
  border: 1px solid rgba(32, 156, 238, 0.4);
  border-radius: 12px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  pointer-events: auto;
  transition: all 0.3s ease;
  overflow: hidden;
  box-sizing: border-box;
}

/* 每列的两个面板间隔30px，高度自适应 */
.left-panels .panel,
.right-panels .panel {
  flex: 1;
  min-height: 0; /* 允许面板收缩 */
}

.left-panels .panel:first-child,
.right-panels .panel:first-child {
  margin-bottom: 30px;
}

.panel:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
  border-color: rgba(32, 156, 238, 0.6);
}

.panel-header {
  background: linear-gradient(90deg, 
    rgba(32, 156, 238, 0.2) 0%,
    rgba(32, 156, 238, 0.05) 100%);
  padding: 12px 16px;
  border-bottom: 1px solid rgba(32, 156, 238, 0.2);
}

.panel-header h3 {
  margin: 0;
  color: #6bc9ff;
  font-size: 14px;
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.panel-content {
  padding: 16px;
  height: calc(100% - 49px); /* 减去header高度 */
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

/* 传感器项目样式 */
.sensor-item,
.alarm-item,
.device-item,
.data-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  flex-shrink: 0;
}

.sensor-item:last-child,
.alarm-item:last-child,
.device-item:last-child,
.data-item:last-child {
  border-bottom: none;
}

.sensor-label,
.data-label {
  color: #b4e1ff;
  font-size: 12px;
}

.sensor-value,
.data-value {
  color: #fff;
  font-size: 12px;
  font-weight: 600;
}

/* 报警类型样式 */
.alarm-type {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
}

.alarm-type.warning {
  background: rgba(255, 193, 7, 0.3);
  color: #ffc107;
  border: 1px solid rgba(255, 193, 7, 0.5);
}

.alarm-type.danger {
  background: rgba(220, 53, 69, 0.3);
  color: #dc3545;
  border: 1px solid rgba(220, 53, 69, 0.5);
}

.alarm-type.info {
  background: rgba(23, 162, 184, 0.3);
  color: #17a2b8;
  border: 1px solid rgba(23, 162, 184, 0.5);
}

.alarm-desc {
  color: #fff;
  font-size: 12px;
  flex: 1;
  margin-left: 8px;
}

/* 设备状态样式 */
.device-status {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 8px;
}

.device-status.online {
  background: #28a745;
  box-shadow: 0 0 8px rgba(40, 167, 69, 0.6);
}

.device-status.offline {
  background: #6c757d;
  box-shadow: 0 0 8px rgba(108, 117, 125, 0.6);
}

.device-name {
  color: #b4e1ff;
  font-size: 12px;
  flex: 1;
}

.device-status-text {
  color: #fff;
  font-size: 11px;
  font-weight: 600;
}

/* 覆盖内容样式 */
.overlay-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 10;
  text-align: center;
  color: #fff;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
  pointer-events: none;
}

.overlay-content h1 {
  font-size: 2.5rem;
  margin-bottom: 10px;
  font-weight: 700;
}

.overlay-content p {
  font-size: 1.2rem;
  margin-bottom: 30px;
  opacity: 0.9;
}

.map-controls {
  display: flex;
  gap: 12px;
  justify-content: center;
  pointer-events: auto;
}

.control-btn {
  background: linear-gradient(135deg, 
    rgba(32, 156, 238, 0.8) 0%,
    rgba(11, 77, 124, 0.8) 100%);
  color: white;
  border: 1px solid rgba(32, 156, 238, 0.6);
  padding: 10px 20px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
  backdrop-filter: blur(5px);
  font-weight: 600;
}

.control-btn:hover {
  background: linear-gradient(135deg, 
    rgba(32, 156, 238, 1) 0%,
    rgba(11, 77, 124, 1) 100%);
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(32, 156, 238, 0.4);
}

/* 适配移动端 */
@media (max-width: 768px) {
  .left-panels,
  .right-panels {
    padding: 10px;
  }
  
  .panel {
    width: 240px;
  }
  
  .left-panels .panel:first-child,
  .right-panels .panel:first-child {
    margin-bottom: 15px;
  }
  
  .overlay-content {
    padding: 10px;
  }
  
  .overlay-content h1 {
    font-size: 1.8rem;
  }
  
  .overlay-content p {
    font-size: 1rem;
  }
  
  .map-controls {
    flex-wrap: wrap;
  }
}

/* 小屏幕适配 */
@media (max-width: 480px) {
  .left-panels,
  .right-panels {
    position: static;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    gap: 15px;
    height: auto;
  }
  
  .panel {
    width: calc(50% - 20px);
    min-width: 200px;
    flex: none;
  }
  
  .left-panels .panel:first-child,
  .right-panels .panel:first-child {
    margin-bottom: 0;
  }
}
</style>